package com.meilingcloud.web.controller.oss;

import com.meilingcloud.common.config.MinioConfig;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.utils.DateUtils;
import com.meilingcloud.common.utils.uuid.IdUtils;
import com.meilingcloud.system.service.IMinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

@RequestMapping("/minio")
@RestController
@Api(tags = "文件上传下载接口")
public class MinioController {

    @Autowired
    private IMinioService minioService;

    @Autowired
    private MinioConfig minioConfig;

    @ApiOperation(value = "使用minio文件上传")
    @PostMapping("/uploadFile")
    @ApiImplicitParam(dataType = "MultipartFile", name = "file", value = "上传的文件", required = true)
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            String bucketName = minioConfig.getBucketName();
            if (!minioService.bucketExists(bucketName)) {
                minioService.makeBucket(bucketName);
            }
            String fileName = file.getOriginalFilename();
            String objectName = DateUtils.dateTimeNow() + "-" + IdUtils.simpleUUID() + fileName.substring(fileName.lastIndexOf("."));

            InputStream inputStream = file.getInputStream();
            minioService.putObject(bucketName, objectName, inputStream, file.getContentType());
            inputStream.close();
            return AjaxResult.success("上传成功", objectName);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("上传失败");
        }
    }

    @ApiOperation(value = "删除minio中的文件")
    @DeleteMapping("/deleteFile/{objectName}")
    @ApiImplicitParam(dataType = "String", name = "objectName", value = "文件名称", required = true)
    public AjaxResult deleteFile(@PathVariable("objectName") String objectName) {
        try {
            String bucketName = minioConfig.getBucketName();
            minioService.removeObject(bucketName, objectName);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败");
        }
    }

    @ApiOperation(value = "获取文件预览地址")
    @GetMapping("/getPreviewUrl/{objectName}")
    public AjaxResult getPreviewUrl(@PathVariable("objectName") String objectName) {
        return AjaxResult.success("success", minioService.presignedGetObject(minioConfig.getBucketName(), objectName, 7 * 24 * 3600));
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/downloadFile/{objectName}")
    public void downloadFile(@PathVariable("objectName") String objectName, HttpServletResponse response) {
        minioService.downloadFile(minioConfig.getBucketName(), objectName, response);
    }

}
